package naga.x.platform2;

import java.util.HashSet;
import java.util.Set;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

import naga.x.platform2.support.FastJsonHttpMessageConverterImpl;
import naga.x.platform2.utils.IOUtils;
import naga.x.platform2.utils.IOUtils.LineReaderListener;
import naga.x.platform2.utils.WindowsShutdownListener;

@SpringBootApplication
@ServletComponentScan
public class App implements EmbeddedServletContainerCustomizer, InitializingBean {

	public static final Logger LOG = LoggerFactory.getLogger(App.class);
	
	@Value("${app.debug}")
	private boolean debugMode;
	@Value("${app.url}")
	private String url;
	@Value("${app.resourceServerUrl}")
	private String resourceServerUrl; // 资源缓存服务器（存储图片）
	
	private Set<String> wanbaOldUsers = new HashSet<>(1024);
	
	private static App instance;
	
	public App() {
		instance = this;
	}
	
	public static void main(String[] args) {
		WindowsShutdownListener.listen();
		SpringApplication.run(App.class, args);
	}
	
	public static App getInstance() {
		return instance;
	}
	
	@Override
	public void customize(ConfigurableEmbeddedServletContainer container) {
	}
	
	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource getDataSource() {
		return new DruidDataSource();
	}
	
	@Bean
	public DataSourceTransactionManager getTransactionManager(DataSource ds) {
		return new DataSourceTransactionManager(ds);
	}
	
	@Bean
	public SqlSessionFactory getSessionFactory(DataSource ds) throws Exception {
		SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
		factory.setDataSource(ds);
		return factory.getObject();
	}
	
	@Bean
	public HttpMessageConverters getConverters() {
		HttpMessageConverter<?> fastjson = new FastJsonHttpMessageConverterImpl();
		return new HttpMessageConverters(fastjson);
	}

	public boolean isDebugMode() {
		return debugMode;
	}

	public String getUrl() {
		return url;
	}
	
	public String getResourceServerUrl() {
		return resourceServerUrl;
	}

	public Set<String> getWanbaOldUsers() {
		return wanbaOldUsers;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		IOUtils.readLineByLine(IOUtils.getResourceAsStream("wanba_old_users.txt"), IOUtils.UTF8, new LineReaderListener() {
			@Override
			public boolean readLine(String line) {
				wanbaOldUsers.add(line);
				return true;
			}
		});
		System.out.println(wanbaOldUsers.size());
		LOG.info("[APP LOADED]DEBUG[{}]URL[{}]", debugMode, url);
	}
	
}
